package com.fuli.app.service.Serviceimpl;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.Pagination;
import com.fuli.app.common.SystemContext;
import com.fuli.app.dao.ReviewDao;
import com.fuli.app.dto.MessageDTO;
import com.fuli.app.dto.ReviewDTO;
import com.fuli.app.dto.query.ReviewQueryDTO;
import com.fuli.app.entity.Message;
import com.fuli.app.entity.Review;
import com.fuli.app.service.ReviewService;
import com.fuli.app.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReviewServiceImpl implements ReviewService {
    @Autowired
    private ReviewDao reviewDao;
    @Override
    public ActionResult<String> add(ReviewDTO dto) {
       Review review = EntityUtils.copyObjectWithInit(dto, Review.class);
       review.setId(EntityUtils.uuid());
       reviewDao.insert(review);
        return new ActionResult<>(review.getId());
    }

    @Override
    public ActionResult<String> delete(String id) {
        Review review = reviewDao.get(id);
        if (review == null)
            return new ActionResult<>(id);
        reviewDao.delete(id, SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<List<String>> batchDelete(List<String> ids) {
        reviewDao.batchDelete(ids,SystemContext.getUserId());
        return new ActionResult<>(ids);
    }

    @Override
    public ActionResult<ReviewDTO> get(String id) {
        Review review =reviewDao.get(id);
        ReviewDTO reviewDTO = EntityUtils.copyObject(review,ReviewDTO.class);
        return new ActionResult<>(reviewDTO);
    }

    @Override
    public ActionResult<Pagination<ReviewDTO>> listByQueryDTO(ReviewQueryDTO queryDTO) {
        Pagination pagination = new Pagination(queryDTO.getPageNo(), queryDTO.getPageSize());
        int count = reviewDao.getCountByQuery(queryDTO);
        pagination.setCount(count);
        if (count > 0) {
            List<Review> reviews = reviewDao.listByQueryDTO(queryDTO);
            List<ReviewDTO> dtos = EntityUtils.copyList(reviews,ReviewDTO.class);
            pagination.setRows(dtos);
            return new ActionResult<>(pagination);
        }
        return new ActionResult<>(pagination);
    }

    @Override
    public ActionResult<List<ReviewDTO>> getByWorkId(String workId) {
        List<Review> review =reviewDao.getByWorkId(workId);
        List<ReviewDTO> reviewDTO = EntityUtils.copyList(review,ReviewDTO.class);
        return new ActionResult<>(reviewDTO);
    }

    @Override
    public ActionResult<List<ReviewDTO>> getByUserId(String userId) {
        List<Review> review =reviewDao.getByUserId(userId);
        List<ReviewDTO> reviewDTO = EntityUtils.copyList(review,ReviewDTO.class);
        return new ActionResult<>(reviewDTO);
    }
}
